package com.ljpz.manage.controller;

import com.ljpz.common.bean.EasyUIResult;
import com.ljpz.manage.pojo.Content;
import com.ljpz.manage.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import sun.misc.Contended;

/**
 * Created by liujunchen on 2016/12/11.
 */
@RequestMapping("content")
@Controller
public class ContentController {
    @Autowired
    private ContentService  contentService   ;

    /**
     *新增内容
     * @param content
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Void> saveContent(Content content) {
        try {
            content.setId(null);
            this.contentService.save(content) ;
            return  ResponseEntity.status(HttpStatus.CREATED).build();
        }catch (Exception e ){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     *分页处理
     */
    @RequestMapping(method = RequestMethod.GET)
    public  ResponseEntity<EasyUIResult>queryListByCategoryId(@RequestParam("categoryId")Long categoryId ,
                                                              @RequestParam(value = "page",defaultValue = "1")Integer page ,
                                                              @RequestParam(value = "rows",defaultValue = "10")Integer rows){
        try {
            EasyUIResult   easyUIResult  =  this.contentService.queryListByCategoryId(categoryId , page, rows)   ;
            return    ResponseEntity.ok(easyUIResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null) ;

    }
}

